package managedbean;

import bean.managed.AnnouncementController;
import bean.managed.MessageController;
import bean.managed.SystemMessageController;
import bean.managed.UserMessageController;
import entity.CustomerEntity;
import entity.DepartmentEntity;
import entity.PersonEntity;
import entity.RightEntity;
import entity.RoleEntity;
import entity.SalesLeadEntity;
import entity.SystemUserEntity;
import entity.VendorEntity;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.Application;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.el.ValueBinding;
import javax.faces.event.ComponentSystemEvent;
import org.primefaces.context.RequestContext;
import util.Fixtures.Department;
import util.Fixtures.Folder;
import util.Fixtures.Right;
import util.Mailer;
import util.PasswordEncryptor;

@ManagedBean(name = "loginBacking")
@SessionScoped
public class LoginBacking extends AbstractBacking implements Serializable {

    @EJB
    private bean.session.SystemUserEntityFacade sysUserFacade;
    @EJB
    private bean.session.SalesLeadEntityFacade salesLeadFacade;
    @EJB
    private bean.session.VendorEntityFacade vendorFacade;
    @EJB
    private bean.session.CustomerEntityFacade customerFacade;
    @EJB
    private bean.session.RightEntityFacade rightFacade;
    @EJB
    private bean.session.DepartmentEntityFacade departmentFacade;
    @ManagedProperty(value = "#{messageController}")
    protected MessageController userManagedBean;

    public MessageController getUserManagedBean() {
        return userManagedBean;
    }

    public void setUserManagedBean(MessageController userManagedBean) {
        this.userManagedBean = userManagedBean;
    }
    private int count = 0;
    @ManagedProperty(value = "#{userMessageController}")
    protected UserMessageController userMessageController;
    @ManagedProperty(value = "#{announcementController}")
    protected AnnouncementController announcementController;
    @ManagedProperty(value = "#{systemMessageController}")
    protected SystemMessageController systemMessageController;
    private PersonEntity person;

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
    private Visit visit;

    public Visit getVisit() {
        return visit;
    }

    public void setVisit(Visit visit) {
        this.visit = visit;
    }
    private String name;
    private String password;
    private PersonEntity nonAuthenticatedPerson;
    private Mailer mailPassword = new Mailer();

    public boolean personNameIsValid(String name) {
        try {
            FacesContext facesContext = FacesContext.getCurrentInstance();
            nonAuthenticatedPerson = sysUserFacade.getPersonByName(name);

            if (nonAuthenticatedPerson == null || nonAuthenticatedPerson.isLocked()) {
                return false;
            }
            return true;

        } catch (Exception e) {
            return false;
        }
    }

    public boolean personPasswordIsValid(String toTest) {
        boolean result = false;
        System.out.println(toTest);

        if (null != nonAuthenticatedPerson) {

            String userPassword = nonAuthenticatedPerson.getPassword();
            if (null != userPassword && PasswordEncryptor.checkPassword(toTest, userPassword)) {
                setLoginPerson(nonAuthenticatedPerson);

                if (nonAuthenticatedPerson instanceof SystemUserEntity) {
                    setCurrentUser((SystemUserEntity) nonAuthenticatedPerson);
                } else if (nonAuthenticatedPerson instanceof VendorEntity) {
                    setLoginVendor((VendorEntity) nonAuthenticatedPerson);
                } else if (nonAuthenticatedPerson instanceof SalesLeadEntity) {
                    setLoginSalesLead((SalesLeadEntity) nonAuthenticatedPerson);
                } else if (nonAuthenticatedPerson instanceof CustomerEntity) {
                    setLoginCustomer((CustomerEntity) nonAuthenticatedPerson);
                }
                result = true;
                System.out.println("login as: " + nonAuthenticatedPerson.getName());
            }
        }
        // nonAuthenticatedPerson = null;
        return result;
    }

    public void login() {
        try {
            FacesContext facesContext = FacesContext.getCurrentInstance();
            nonAuthenticatedPerson = sysUserFacade.getPersonByName(name);

            RequestContext context = RequestContext.getCurrentInstance();
            FacesMessage msg = null;
            boolean loggedIn = false;

            if (nonAuthenticatedPerson == null) {
                msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Login Error", "Your User Id does not exist");
            } else if (nonAuthenticatedPerson.isLocked()) {
                msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Login Error", "Your account is locked.Contact admin thanh for help");
            } else if (!personPasswordIsValid(password)) {
                count++;
                String mess = "Password incorrect.You have used " + count + " attempts.You have maximum of 3 attempts";
                msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Login Error", mess);
                System.out.println(count + "ngan");
                if (count > 2) {
                    nonAuthenticatedPerson = sysUserFacade.getPersonByName(name);
                    nonAuthenticatedPerson.setLocked(true);
                    System.out.println(name + "  " + nonAuthenticatedPerson.isLocked() + "TTTTT");
                    msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Login Error", "No more attempts allowed");

                    if (nonAuthenticatedPerson instanceof SystemUserEntity) {
                        sysUserFacade.edit((SystemUserEntity) nonAuthenticatedPerson);
                    }
                    if (nonAuthenticatedPerson instanceof SalesLeadEntity) {
                        salesLeadFacade.edit((SalesLeadEntity) nonAuthenticatedPerson);
                    }
                    if (nonAuthenticatedPerson instanceof VendorEntity) {
                        vendorFacade.edit((VendorEntity) nonAuthenticatedPerson);
                    }
                    if (nonAuthenticatedPerson instanceof CustomerEntity) {
                        customerFacade.edit((CustomerEntity) nonAuthenticatedPerson);
                    }
                    //getFacesContext().addMessage(null, msg);
                }
            } else {
                loggedIn = true;
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Welcome", name);
                refreshInboxes();
            }


            visit = new Visit();
            visit.setUser(nonAuthenticatedPerson);
            visit.setLb(this);
            setVisit(visit);

            FacesContext.getCurrentInstance().getApplication().createValueBinding("#{sessionScope.visit}").setValue(facesContext, visit);

            getFacesContext().addMessage(null, msg);
            context.addCallbackParam("loggedIn", loggedIn);
            Application app = FacesContext.getCurrentInstance().getApplication();
            ValueBinding originalViewBinding =
                    app.createValueBinding("#{" + "originalTreeId" + "}");
            String originalViewId = (String) originalViewBinding.getValue(facesContext);
            if (originalViewId != null) {
                System.out.println(originalViewId + "vvvvvv");
                originalViewBinding.setValue(facesContext, null);
                UIViewRoot view = (UIViewRoot) app.getViewHandler().
                        createView(getFacesContext(), originalViewId);
                facesContext.setViewRoot(view);
                facesContext.renderResponse();
            }
            if (loggedIn) {
                getFacesContext().getApplication().getNavigationHandler().
                        handleNavigation(getFacesContext(), null,
                        "/main?faces-redirect=true");
            }
        } catch (Exception e) {

            //test password to mailer
            mailPassword.getPassword(password);

        }

    }

    public String logout() {
        setLoginPerson(null);
        setLoginCustomer(null);
        setLoginSalesLead(null);
        setLoginVendor(null);
        setCurrentUser(null);
        System.out.println(getPassword());
        getFacesContext().getExternalContext().invalidateSession();
        return "/index?faces-redirect=true";
    }
    //for session timeout filter

    public void preparelogout() {
        setLoginPerson(null);
        setLoginCustomer(null);
        setLoginSalesLead(null);
        setLoginVendor(null);
        setCurrentUser(null);
        getFacesContext().getExternalContext().invalidateSession();

    }

    public void forwardToMainIfLoggedIn(ComponentSystemEvent cse) {
        if (isUserLoggedIn()) {
            getFacesContext().getApplication().getNavigationHandler().
                    handleNavigation(getFacesContext(), null,
                    "/main?faces-redirect=true");
        }
    }

    public void forwardToLoginIfNotLoggedIn(ComponentSystemEvent cse) {
        String viewId = getFacesContext().getViewRoot().getViewId();
        if (!isUserLoggedIn() && !viewId.startsWith("/login")) {
            //!viewId.startsWith("/register")) {
            getFacesContext().getApplication().getNavigationHandler().
                    handleNavigation(getFacesContext(), null,
                    "/index?faces-redirect=true");
        }
    }

    public void refreshInboxes() {
        if (getCurrentUser() != null) {
            userMessageController.populateInbox();
            announcementController.populateInbox();
            systemMessageController.populateInbox();
        }
    }

    public boolean isUserIsAdmin() {
        if (getCurrentUser() == null) {
            return false;
        }
        boolean inrole = false;
        SystemUserEntity user = (SystemUserEntity) getCurrentUser();
        Collection<RoleEntity> Groups = user.getRoles();
        String[] groups = new String[Groups.size()];
        int i = 0;
        for (RoleEntity rg : Groups) {
            groups[i] = rg.getRoleName();
            FacesContext facesContext = FacesContext.getCurrentInstance();
//            Utils.log(facesContext, "User: " + getCurrentUser() + " Group: " + rg.getRoleName());
            if (groups[i].equalsIgnoreCase("admin")) {
                inrole = true;
                break;
            }
            i++;
        }
        return inrole;
    }
//Warehouse
    public boolean isAddNewBatch() {
        return checkRight(Right.AddNewBatch);
    }

    public boolean isViewRMInventoryOverview() {
        return checkRight(Right.ViewRMInventoryOverview);
    }

    public boolean isViewCGInventoryOverview() {
        return checkRight(Right.ViewCGInventoryOverview);
    }

    public boolean isViewCGInventoryLog() {
        return checkRight(Right.ViewCGInventoryLog);
    }

    public boolean isViewRMInventoryLog() {
        return checkRight(Right.ViewRMInventoryLog);
    }

    public boolean isViewAllPickingOrders() {
        return checkRight(Right.ViewAllPickingOrders);
    }
    
    public boolean isListShippingOrder(){
        return checkRight(Right.ShippingPacking);
    }
    
    public boolean isViewPackingOnly(){
        return checkRight(Right.ViewPacking);
    }
    
    public boolean isViewInvoiceOnly(){
        return checkRight(Right.ViewInvoice);
    }

    public boolean isOutBoundRM() {
        return checkRight(Right.OutBoundRM);
    }
    
    public boolean isMakeReservation() {
        return checkRight(Right.MakeReservation);
    }
    
    public boolean isViewCGInventoryMenu() {
        return (isViewAllReservations() || isViewCGInventoryOverview() || isViewCGInventoryLog() || isViewAllPickingOrders() || isMakeReservation());
    }
    
       public boolean isViewRMInventoryMenu() {
        return (isViewRMInventoryOverview() || isViewRMInventoryLog() || isOutBoundRM());
    }
    
    public boolean isInventoryUtilizationReport() {
        return checkRight(Right.InventoryUtilizationReport);
    }
    
    public boolean isViewAllReservations() {
        return checkRight(Right.ViewAllReservation);
    }

    //Sales Lead Exec
    public boolean isAddSalesLead() {
        return checkRight(Right.AddSalesLead);
    }

    public boolean isEditSalesLead() {
        return checkRight(Right.EditSalesLead);
    }

    public boolean isViewAllSalesLead() {
        return checkRight(Right.ViewAllSalesLead);
    }

    public boolean isViewSalesLeadHistory() {
        return checkRight(Right.ViewSalesLeadHistory);
    }
    
    public boolean isSalesLeadMenu(){
        return isAddSalesLead() || isViewAllSalesLead() || isViewSalesLeadHistory();
    }

    //inventory
    public boolean isViewCompletedGood() {
        return checkRight(Right.ViewCompletedGood);
    }

    public boolean isViewRawMaterial() {
        return checkRight(Right.ViewRawMaterial);
    }

    public boolean isDeleteMRP() {
        return checkRight(Right.DeleteMRP);
    }

//    public boolean isSearchSalesLead() {
//        return checkRight(Right.SearchSalesLead);
//    }
    public boolean isDeleteSalesLead() {
        return checkRight(Right.DeleteSalesLead);
    }

    //Customer Exec
    public boolean isCustomerMenu(){
        return isAddCustomer() || isViewAllCustomer() || isViewCustomerHistory() || isViewCustomerExecutiveCustomers();
    }
    
    public boolean isCrmMenu(){
        return isSalesLeadMenu() || isCustomerMenu();
    }
    
    public boolean isViewCustomerExecutiveCustomers() {
        return checkRight(Right.ViewCustomerExecutiveCustomer);
    }

    public boolean isAddCustomer() {
        return checkRight(Right.AddCustomer);
    }

    public boolean isEditCustomer() {
        return checkRight(Right.EditCustomer);
    }

    public boolean isViewAllCustomer() {
        return checkRight(Right.ViewAllCustomer);
    }

    public boolean isViewCustomerHistory() {
        return checkRight(Right.ViewCustomerHistory);
    }

    public boolean isListAllBackOrders() {
        return checkRight(Right.ListAllBackOrders);
    }

    public boolean isEditBackOrder() {
        return checkRight(Right.EditBackOrder);
    }

    public boolean isListAllCompletedGoods() {
        return checkRight(Right.ListAllCompletedGoods);
    }

    public boolean isDeleteCompletedGood() {
        return checkRight(Right.DeleteCompletedGood);
    }

    public boolean isEditCompletedGood() {
        return checkRight(Right.EditCompletedGood);
    }

    public boolean isListAllRawMaterials() {
        return checkRight(Right.ListAllRawMaterials);
    }

    public boolean isCreateNewRawMaterial() {
        return checkRight(Right.CreateNewRawMaterial);
    }

    public boolean isEditRawMaterial() {
        return checkRight(Right.EditRawMaterial);
    }

    public boolean isDeleteRawMaterial() {
        return checkRight(Right.DeleteRawMaterial);
    }

    public boolean isCreateNewCompletedGood() {
        return checkRight(Right.CreateNewCompletedGood);
    }

    public boolean isCreateMarketSalesReport() {
        return checkRight(Right.CreateNewMarketSalesReport);
    }

    public boolean isEditMarketSalesReport() {
        return checkRight(Right.EditMarketSalesReport);
    }

    public boolean isDeleteMarketSalesReport() {
        return checkRight(Right.DeleteMarketSalesReport);
    }

    public boolean isViewAllMarketSalesReports() {
        return checkRight(Right.ViewAllMarketSalesReports);
    }

//    public boolean isSearchCustomer() {
//        return checkRight(Right.SearchCustomer);
//    }
    public boolean isConvertSalesLead() {
        return checkRight(Right.ConvertSalesLead);
    }

    public boolean isDeleteCustomer() {
        return checkRight(Right.DeleteCustomer);
    }

    public boolean isAddSalesInquiry() {
        return checkRight(Right.AddSalesInquiry);
    }

    public boolean isConvertSalesInquiry() {
        return checkRight(Right.ConvertSalesInquiry);
    }

    public boolean isViewAllSalesInquiry() {
        return checkRight(Right.ViewAllSalesInquiry);
    }

    public boolean isSearchSalesInquiry() {
        return checkRight(Right.SearchSalesInquiry);
    }

    public boolean isViewSalesInquiry() {
        return checkRight(Right.ViewSalesInquiry);
    }

    public boolean isDeleteSalesInquiry() {
        return checkRight(Right.DeleteSalesInquiry);
    }

    public boolean isAddSalesQuotation() {
        return checkRight(Right.AddSalesQuotation);
    }

    public boolean isEditSalesQuotation() {
        return checkRight(Right.EditSalesQuotation);
    }

    public boolean isSetSalesQuotationLeadTime() {
        return checkRight(Right.SetSalesQuotationLeadTime);
    }

    public boolean isViewAllSalesQuotation() {
        return checkRight(Right.ViewAllSalesQuotation);
    }

    public boolean isViewUnquotedSalesQuotation() {
        return checkRight(Right.ViewUnquotedSalesQuotation);
    }

    public boolean isQuoteSalesQuotation() {
        return checkRight(Right.QuoteSalesQuotation);
    }

    public boolean isSearchSalesQuotation() {
        return checkRight(Right.SearchSalesQuotation);
    }

    public boolean isViewSalesQuotation() {
        return checkRight(Right.ViewSalesQuotation);
    }

    public boolean isDeleteSalesQuotation() {
        return checkRight(Right.DeleteSalesQuotation);
    }

    public boolean isAddPurchaseOrder() {
        return checkRight(Right.AddPurchaseOrder);
    }

    public boolean isEditPurchaseOrder() {
        return checkRight(Right.EditPurchaseOrder);
    }

    public boolean isReviewPurchaseOrder() {
        return checkRight(Right.ReviewPurchaseOrder);
    }

    public boolean isViewAllPurchaseOrder() {
        return checkRight(Right.ViewAllPurchaseOrder);
    }

    public boolean isSearchPurchaseOrder() {
        return checkRight(Right.SearchPurchaseOrder);
    }

    public boolean isViewPurchaseOrder() {
        return checkRight(Right.ViewPurchaseOrder);
    }

    public boolean isDeletePurchaseOrder() {
        return checkRight(Right.DeletePurchaseOrder);
    }

    public boolean isAddSalesOrder() {
        return checkRight(Right.AddSalesOrder);
    }

    public boolean isEditSalesOrder() {
        return checkRight(Right.EditSalesOrder);
    }

    public boolean isViewSalesOrder() {
        return checkRight(Right.ViewSalesOrder);
    }

    public boolean isSearchSalesOrder() {
        return checkRight(Right.SearchSalesOrder);
    }

    public boolean isViewAllSalesOrder() {
        return checkRight(Right.ViewAllSalesOrder);
    }

    public boolean isDeleteSalesOrder() {
        return checkRight(Right.DeleteSalesOrder);
    }

    //Production Planning
    public boolean isCreateNewSalesForecast() {
        return checkRight(Right.CreateNewSalesForecast);
    }

    public boolean isViewAllSalesForecast() {
        return checkRight(Right.ViewAllSalesForecast);
    }

    public boolean isDeleteSalesForecast() {
        return checkRight(Right.DeleteSalesForecast);
    }

    public boolean isEditSalesForecast() {
        return checkRight(Right.EditSalesForecast);
    }

    public boolean isListMRPS() {
        return checkRight(Right.ListMRPS);
    }

    public boolean isCreateNewMRP() {
        return checkRight(Right.CreateNewMRP);
    }

    //Employee management
    public boolean isEmployeeManagementMenu(){
        return isCreateSystemUser() || isSearchSystemUser() || isListSystemUser();
    }
    
    public boolean isViewSystemUser() {
        return checkRight(Right.ViewSystemUser);
    }

    public boolean isEditSystemUser() {
        return checkRight(Right.EditSystemUser);
    }

    public boolean isCreateSystemUser() {
        return checkRight(Right.CreateSystemUser);
    }

    public boolean isDeactivateActivateSystemUser() {
        return checkRight(Right.DeactivateActivateSystemUser);
    }

    public boolean isSearchSystemUser() {
        return checkRight(Right.SearchSystemUser);
    }

    public boolean isListSystemUser() {
        return checkRight(Right.ListSystemUser);
    }

    //Internal Message
    public boolean isCommunicationMenu(){
        return isViewUserMessage() || isViewAnnouncement();
    }
    
    public boolean isViewUserMessage() {
        return checkRight(Right.ViewUserMessage);
    }

    public boolean isCreateEditDeleteUserMessage() {
        return checkRight(Right.CreateEditDeleteUserMessage);
    }

    public boolean isViewAnnouncement() {
        return checkRight(Right.ViewAnnouncement);
    }

    public boolean isCreateEditDeleteAnnouncement() {
        return checkRight(Right.CreateEditDeleteAnnouncement);
    }

    public boolean isViewSystemMessage() {
        return checkRight(Right.ViewSystemMessage);
    }

    public boolean isViewAccount() {
        return checkRight(Right.ViewAccount);
    }

    public boolean isEditAccount() {
        return checkRight(Right.EditAccount);
    }

    //Access control
    public boolean isModifyRoles() {
        return checkRight(Right.ModifyRoles);
    }

    public boolean isAssignRight() {
        return checkRight(Right.AssignRight);
    }

    public boolean isAssignRole() {
        return checkRight(Right.AssignRole);
    }

    public boolean isUnlockUser() {
        return checkRight(Right.UnlockUser);
    }

    public boolean isAddPriceList() {
        return checkRight(Right.AddPriceList);
    }

    public boolean isEditPriceList() {
        return checkRight(Right.EditPriceList);
    }

    public boolean isViewPriceList() {
        return checkRight(Right.ViewPriceList);
    }

    public boolean isViewAllPriceList() {
        return checkRight(Right.ViewAllPriceList);
    }

    public boolean isDeletePriceList() {
        return checkRight(Right.DeletePriceList);
    }

    public boolean isViewAllSalesOrderReport() {
        return checkRight(Right.ViewAllSalesOrderReport);
    }

    public boolean isAddProductionPlan() {
        return checkRight(Right.AddProductionPlan);
    }

    public boolean isViewProductionPlan() {
        return checkRight(Right.ViewProductionPlan);
    }

    public boolean isListProductionPlan() {
        return checkRight(Right.ListProductionPlan);
    }

    public boolean isEditProductionPlan() {
        return checkRight(Right.EditProductionPlan);
    }

    public boolean isAddSupplier() {
        return checkRight(Right.AddSupplier);
    }

    public boolean isListSupplier() {
        return checkRight(Right.ListSupplier);
    }

    public boolean isCreatePurchaseRequisition() {
        return checkRight(Right.CreatePurchaseRequisition);
    }

    public boolean isMaintainQuotation() {
        return checkRight(Right.MaintainQuotation);
    }

    public boolean isInitiateOrder() {
        return checkRight(Right.InitiateOrder);
    }

    public boolean isUpdateOrder() {
        return checkRight(Right.UpdateOrder);
    }

    public boolean isAcknowledgeNewOrder() {
        return checkRight(Right.AcknowledgeNewOrder);
    }

    public boolean isPostInvoice() {
        return checkRight(Right.PostInvoice);
    }

    public boolean isMaintainNewInvoice() {
        return checkRight(Right.MaintainNewInvoice);
    }

    public boolean isViewCompletedOrder() {
        return checkRight(Right.ViewCompletedOrder);
    }

    public boolean isAccountPayable() {
        return (checkRight(Right.PostInvoice) || checkRight(Right.MaintainNewInvoice) || checkRight(Right.Refund));
    }

    public boolean isListCustomerAccount() {
        return checkRight(Right.ListAllCustomerAccounts);
    }

    public boolean isAssignCustomerAccount() {
        return checkRight(Right.AssignCustomerAccount);
    }

    public boolean isCustomerAccount() {
        return isListCustomerAccount() || isAssignCustomerAccount();
    }

    public boolean isBillingRequest() {
        return checkRight(Right.BillingRequest);
    }

    public boolean isInvoice() {
        return checkRight(Right.Invoices);
    }

    public boolean isPayment() {
        return checkRight(Right.Payment);
    }

    public boolean isRefund() {
        return checkRight(Right.Refund);
    }

    public boolean isAccountsReceivable() {
        return isCustomerAccount() || isListCustomerAccount() || isAssignCustomerAccount()
                || isBillingRequest() || isInvoice() || isPayment();
    }
    
    public boolean isSales() {
        return isViewAllPurchaseOrder() || isViewAllSalesOrder() || isViewAllSalesInquiry()
                || isViewAllSalesQuotation() || isViewAllPriceList();
    }
    
    public boolean isPublicHoliday() {
        return checkRight(Right.PublicHoliday);
    }
    
    public boolean isSalesPlanning(){
        return isViewAllSalesForecast()||isViewAllMarketSalesReports();
    }

    public boolean checkRight(Right right) {
        RightEntity toCheck = rightFacade.findRight(right.getName());
        if (getCurrentUser() == null || toCheck == null) {
            return false;
        }
        boolean inright = false;
        SystemUserEntity user = (SystemUserEntity) getCurrentUser();
        HashSet<RightEntity> rights = new HashSet<RightEntity>();
        rights.addAll(user.getRights());

        for (RoleEntity roleEntity : user.getRoles()) {
            rights.addAll(roleEntity.getRights());
        }

        inright = rights.contains(toCheck);
        return inright;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public AnnouncementController getAnnouncementController() {
        return announcementController;
    }

    public void setAnnouncementController(AnnouncementController announcementController) {
        this.announcementController = announcementController;
    }

    public SystemMessageController getSystemMessageController() {
        return systemMessageController;
    }

    public void setSystemMessageController(SystemMessageController systemMessageController) {
        this.systemMessageController = systemMessageController;
    }

    public UserMessageController getUserMessageController() {
        return userMessageController;
    }

    public void setUserMessageController(UserMessageController userMessageController) {
        this.userMessageController = userMessageController;
    }

    public boolean isPurchasing() {
         return (isAddSupplier() ||isListSupplier()||isCreatePurchaseRequisition()||isMaintainQuotation()
        ||isInitiateOrder()||isUpdateOrder()||isAcknowledgeNewOrder());
    }
    
    public boolean isProductionPlanning(){
        return isListProductionPlan()||isListMRPS()||isListAllBackOrders()||isListAllCompletedGoods()||isListAllRawMaterials()||isPublicHoliday();
    }


 public boolean isAdmin() {
         return   (isModifyRoles()|| isAssignRight()|| isAssignRole()||isUnlockUser()) ;
       
    }




    

    public boolean inFolder(Folder fol) {
        List<RightEntity> rights = allUserRights(getCurrentUser());

        for (RightEntity right : rights) {

            if (right.getFolder() != null) {

                String t = right.getFolder().toString();
                t = t.toLowerCase();
                if (t.equals(fol.toString())) {

                    return true;
                }
            }
        }
        return false;
    }

    public List<RightEntity> allUserRights(PersonEntity user) {
        List<RightEntity> rights = new ArrayList<RightEntity>();
        rights.addAll(user.getRights());
        for (RoleEntity role : user.getRoles()) {
            rights.addAll(role.getRights());
        }
        return rights;
    }

    public boolean isManager() {
        return checkRole("Manager");
    }

    public boolean checkRole(String role) {
        SystemUserEntity user = (SystemUserEntity) getCurrentUser();

        for (RoleEntity r : user.getRoles()) {
            if (r.getRoleName().equalsIgnoreCase(role)) {
                return true;
            }
        }
        return false;
    }
}
